name: Python application

on:
  push:
    branches: [ "pipeline", "pipeline_tags" ]
  pull_request:
    branches: [ "main" ]

env:
  DB: ${{ secrets.DB_NAME }}
  HOST: ${{ secrets.DB_HOST }}
  LOGIN: ${{ secrets.DB_LOGIN }}
  PASSWORD: ${{ secrets.DB_PASSWORD }}

permissions:
  contents: read
  packages: write

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Generate build ID
      id: prep
      run: |
          branch=${GITHUB_REF##*/}
          sha=${GITHUB_SHA::8}
          ts=$(date +%s)
          echo "::set-output name=BUILD_ID::${branch}-${sha}-${ts}"
    - uses: actions/checkout@v4
    - name: Set up Python 3.11
      uses: actions/setup-python@v3
      with:
        python-version: "3.11"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 mysqlclient
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
      run: |
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Run main.py
      run: python main.py
    - name: Login to GitHub Container Registry
      uses: docker/login-action@v2
      with:
        registry: ghcr.io
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
    - name: Build and push Docker image
      run: |
        docker build . -t ghcr.io/${{ github.repository_owner }}/base-pipeline:${{ github.sha }}-${{ github.run_number }}
        docker push ghcr.io/${{ github.repository_owner }}/base-pipeline:${{ github.sha }}-${{ github.run_number }}
